package org.example.myleet.p429;

import org.example.myleet.model.Node;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new LinkedList<>();
        if (null == root) {
            return result;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> level = new LinkedList<>();
            for (int i = 0; i < size; ++i) {
                Node node = queue.poll();
                level.add(node.val);
                if (null != node.children && 0 < node.children.size()) {
                    for (Node child : node.children) {
                        queue.offer(child);
                    }
                }
            }
            result.add(level);
        }
        return result;
    }
}
